package com.staginfo.segs.function.registerOrLogin.login.presenter;

import android.os.Handler;

import com.staginfo.segs.business.UserManager;
import com.staginfo.segs.common.ApiAsyncTask;
import com.staginfo.segs.function.registerOrLogin.login.LoginConstruct.LoginConstruct;
import com.staginfo.segs.function.registerOrLogin.login.bean.AuthenticateStatus;
import com.staginfo.segs.function.registerOrLogin.login.model.UserLoginModel;
import com.staginfo.segs.function.registerOrLogin.login.view.UserLoginActivity;
import com.staginfo.segs.util.JSONUtils;
import com.staginfo.segs.util.PhoneUtil;
import com.staginfo.segs.util.UuidUtils;

/**
 * Created by robert.luozizhao on 2017/9/5 0005.
 */

public class UserLoginPresenter implements LoginConstruct.LoginPersenter{

    public static final String TAG="UserLoginPresenter";
    private final UserLoginActivity mUserLonginActivity;
    private final UserLoginModel mUserLoginModel;
    private Handler mHandler = new Handler();

    //02

    public UserLoginPresenter(UserLoginActivity userLoginActivity) {
        mUserLonginActivity = userLoginActivity;
        mUserLoginModel = new UserLoginModel();
    }

    public void login() {
        String username = mUserLonginActivity.getUsername().trim();
        String password = mUserLonginActivity.getPassword().trim();
        String terminalId = UuidUtils.fromString(PhoneUtil.getPhoneIMEI(mUserLonginActivity)).toString();

        getListData(username, password, terminalId, new ApiAsyncTask.ApiRequestListener() {
            @Override
            public void onSuccess(int method, Object obj) {
                AuthenticateStatus authenticateStatus = JSONUtils.string2Obejct(obj.toString(), AuthenticateStatus.class);
                mUserLonginActivity.loginSuccess(authenticateStatus);
            }

            @Override
            public void onError(int method, int statusCode) {
                mUserLonginActivity.loginFailed(method,statusCode);
            }
        });

    }


    @Override
    public void getListData(String name, String password, String terminalId, ApiAsyncTask.ApiRequestListener apiRequestListener) {
        UserManager userManager = new UserManager(mUserLonginActivity);
        userManager.authenticate(name,password,terminalId,apiRequestListener);
    }


}
